import os

from django.http import HttpResponse
from django.shortcuts import render, redirect
from pathlib import Path
# Create your views here.
from ourproject import settings
from personal_center.models import Person_info

BASE_DIR = Path(__file__).resolve().parent.parent




def info(request):
    if request.method == "GET":
        username = request.session['username']
        try:
            name = request.session['username']
            info = Person_info.objects.get(username=name)
            realname = info.realname
            phone = info.phone
            gender = info.gender
            birthday = info.birthday
            marry = info.marry
            education = info.education
            signature = info.signature
        except:
            username = request.session['username']
            realname = ''
            phone = ''
            gender = ''
            birthday = ''
            marry = ''
            education = ''
            signature = ''
        return render(request, 'personal_center/info.html', locals())
    elif request.method == "POST":
        try:
            name = request.session['username']
            info = Person_info.objects.get(username=name)
            info.delete()
            username = request.POST['username']
            realname = request.POST['realname']
            phone = request.POST['phone']
            gender = request.POST['gender']
            birthday = request.POST['birthday']
            marry = request.POST['marry']
            education = request.POST['education']
            signature = request.POST['signature']
            afile = request.FILES['myfile']
            filename = afile.name
            Person_info.objects.create(username=username, realname=realname,
                                       phone=phone, gender=gender, birthday=birthday,
                                       marry=marry, education=education, signature=signature,
                                       myfile=afile)
        except:
            username = request.POST['username']
            realname = request.POST['realname']
            phone = request.POST['phone']
            gender = request.POST['gender']
            birthday = request.POST['birthday']
            marry = request.POST['marry']
            education = request.POST['education']
            signature = request.POST['signature']
            afile = request.FILES['myfile']
            filename = afile.name
            Person_info.objects.create(username=username, realname=realname,
                                       phone=phone, gender=gender, birthday=birthday,
                                       marry=marry, education=education, signature=signature,
                                       myfile=afile
                                       )
    return render(request, 'personal_center/info.html', locals())



def info_check(request):
    # add your code here
    if request.method == "GET":
        try:
            name = request.session['username']
            info = Person_info.objects.get(username=name)
        except:
            username = request.session['username']
            realname = '加密代码'
            phone = '加密代码'
            gender = '加密代码'
            birthday = '加密代码'
            marry = '加密代码'
            education = '加密代码'
            signature = '加密代码'
            afile = os.path.join(BASE_DIR, "/static/images/Default.jpg")
            return render(request, 'personal_center/info_check.html', locals())
        username = request.session['username']
        realname = info.realname
        phone = info.phone
        gender = info.gender
        birthday = info.birthday
        marry = info.marry
        education = info.education
        signature = info.signature
        afile = os.path.join(BASE_DIR, "/media/" + f"{info.myfile}")
        print(afile)
        return render(request, 'personal_center/info_check.html', locals())
    elif request.method == "POST":
        return render(request, 'personal_center/info.html', locals())


def individual(request, person):
    if request.method == "GET":
        try:
            user = Person_info.objects.get(username=person)
        except:
            HttpResponse('无此用户')
        username = user.username
        realname = user.realname
        phone = user.phone
        gender = user.gender
        birthday = user.birthday
        marry = user.marry
        education = user.education
        signature = user.signature
        afile = "/media/" + f"{user.myfile}"
        return render(request, 'personal_center/individual.html', locals())
